<template>
  <el-card>
    <el-row :gutter="20" class="header">
      <el-col :span="4">
        <el-input placeholder="请输入角色名称" clearable v-model="queryForm.name"></el-input>
      </el-col>
      <el-button type="primary" :icon="Search" @click="query">搜索</el-button>
      <el-button type="primary" @click="handleAdd">添加</el-button>
    </el-row>
    <el-row class="header">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="编号" />
        <el-table-column prop="name" label="角色名称" />
        <el-table-column prop="des" label="备注" />
        <el-table-column prop="status" label="状态">
          <template #default="{ row }">
            <el-tag type="success" effect="dark" v-if="row.status==1">启用</el-tag>
            <el-tag type="warning" effect="dark" v-if="row.status==0">停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button type="primary" :icon="User" circle @click="handlePermission(row)"></el-button>
            <el-button type="primary" :icon="Edit" circle @click="handleEdit(row)"></el-button>
            <el-button type="danger" :icon="Delete" circle @click="removeById(row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-pagination
      v-model:current-page="queryForm.pageNum"
      v-model:page-size="queryForm.pageSize"
      :page-sizes="[10, 20, 30, 50]"
      layout="prev, pager, next, jumper, sizes, total"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :background="true"
    />
  </el-card>
  <Dialog v-model="dialogVisible"
          :dialogTitle="dialogTitle"
          v-if="dialogVisible"
          @query="query"
          :dialogRowValue="dialogRowValue" />
  <Permission v-model="permissionVisible"
              v-if="permissionVisible"
              :permissionValue="permissionValue"
              :permissionIdsValue="permissionIdsValue"
              :roleIdValue="roleIdValue" />
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { Search, Edit, Delete, User } from '@element-plus/icons-vue'
import { list, remove, listPermission, listPermissionByRoleId } from '../../../api/role'
import Dialog from './dialog'
import Permission from './permission'
import { ElMessageBox, ElMessage } from 'element-plus'

const queryForm = ref({
  name: '',
  pageNum: 1,
  pageSize: 10
})

const tableData = ref([]) // 数据
const total = ref(0) // 总数
const dialogVisible = ref(false) // 隐藏对话框
const dialogTitle = ref('') // 对话框标题
const permissionVisible = ref(false) // 隐藏对话框

async function query() {
  const resp = await list(queryForm.value)
  tableData.value = resp.data.data.list
  total.value = resp.data.data.total
}

onMounted(() => {
  query()
})

function handleCurrentChange(pageNum) {
  queryForm.value.pageNum = pageNum
  query()
}

function handleSizeChange(pageSize) {
  queryForm.value.pageNum = 1
  queryForm.value.pageSize = pageSize
  query()
}

const dialogRowValue = ref({})
const permissionValue = ref([])
const permissionIdsValue = ref([])
const roleIdValue = ref(0)
// 添加
function handleAdd() {
  dialogTitle.value = '添加'
  dialogRowValue.value = {}
  dialogVisible.value = true
}
// 编辑
function handleEdit(row) {
  dialogTitle.value = '编辑'
  dialogRowValue.value = JSON.parse(JSON.stringify(row))
  dialogVisible.value = true
}
// 编辑权限
async function handlePermission(row) {
  // 查询权限列表
  const permissions = await listPermission()
  permissionValue.value = permissions.data.data
  // 查询角色已有权限
  const permissionIds = await listPermissionByRoleId(row.id)
  permissionIdsValue.value = permissionIds.data.data
  roleIdValue.value = row.id
  permissionVisible.value = true
}

// 删除
function removeById(row) {
  ElMessageBox.confirm(
    '是否确认删除?',
    '确认框',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消'
    }
  ).then(async () => {
      await remove(row.id)
      ElMessage({
        type: 'success',
        message: '删除成功'
      })
      query()
    }).catch(() => {
      /*
      ElMessage({
        type: 'info',
        message: 'Delete canceled'
      })
       */
    })
}
</script>

<style lang="scss" scoped>
.header {
  padding-bottom: 16px;
  box-sizing: border-box;
}
::v-deep .el-input__suffix {
  align-items: center;
}
</style>
